package com.zijunlin.Zxing.Demo.Tab;

import com.zijunlin.Zxing.Demo.R;
import android.os.Bundle;
import android.os.Looper;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RePwdActivity extends Activity {
	private Button m_btnOk;
	private Button m_btnCancel;
	private EditText m_editUsr;
	private EditText m_editOldPswd;
	private EditText m_editNewPswd;
	private EditText m_editConfirm;
	private UdpSocket m_udpsocket=null;
	
	HValue_t m_v;
	private String m_sNewPswd;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_re_pwd);
		
		m_btnOk=(Button)findViewById(R.id.rebtnregok);
	    m_btnCancel=(Button)findViewById(R.id.rebtnregcancel);
		
	    
	    
		m_editUsr=(EditText)findViewById(R.id.reeditregusr);
		m_editOldPswd=(EditText)findViewById(R.id.edioldpswd);
		m_editNewPswd=(EditText)findViewById(R.id.editnewpswd);
		m_editConfirm=(EditText)findViewById(R.id.editreconfirm);
		
		//Integer(((SmtApplication)getApplication()).m_iUsrId).toString();
		;
		m_editUsr.setText(String.valueOf(((SmtApplication)getApplication()).m_iUsrId));
		m_editUsr.setEnabled(false);
		

		
		//m_editOldPswd.setText(((SmtApplication)getApplication()).m_sPswd);
		
		
		
		m_btnOk.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {

				
				m_editOldPswd.getText().toString();
				String sOldePswd=((SmtApplication)getApplication()).m_sPswd;
				if (m_editOldPswd.getText().toString().equals(sOldePswd))
				{
					
					if(m_editNewPswd.getText().length()>=6 && m_editNewPswd.getText().length()<=12)
						m_sNewPswd=m_editNewPswd.getText().toString();
					else
					{
						Toast.makeText(getApplicationContext(), "incorrect new pswd,should be >=6 and <12",Toast.LENGTH_SHORT).show();
						return;
					}
					
				}
				else
				{
					
					Toast.makeText(getApplicationContext(), "incorrect old pswd",Toast.LENGTH_SHORT).show();
					return;
				}
				
				
				
				
				if (m_editNewPswd.getText().toString().equals(m_editConfirm.getText().toString()))
				{
					;
					
				}
				else
				{
					
					Toast.makeText(getApplicationContext(), "not the same new pswd",Toast.LENGTH_SHORT).show();
					return;
				}
				
				
				m_v = new HValue_t();
				
				m_v.head.src.pswd=((SmtApplication)getApplication()).m_sPswd.getBytes();
				m_v.head.src.id=((SmtApplication)getApplication()).m_iUsrId;
				
				m_v.szValue = m_editNewPswd.getText().toString().getBytes();
				m_v.head.len = (short)m_sNewPswd.length();
				m_v.head.cmd=cmd.MSG_USR_MODIFY_PSWD;
				new Thread( new Runnable(){
					
					@Override
					public void run() {
							try {

								UdpSocket udpSocket=new UdpSocket();

								udpSocket.send(m_v);
		
								HValue_t recV=new HValue_t();
								udpSocket.receive(recV);
								
								if(recV.head.cmd==cmd.MSG_USR_MODIFY_PSWD)
								{	


								 if(recV.szValue[0]==cmd.ACK_SUCCESS)
									{
										((SmtApplication)getApplication()).m_sPswd=m_sNewPswd;
			
										udpSocket=null;
										Looper.prepare();
										Toast.makeText(getApplicationContext(), "success",Toast.LENGTH_LONG).show();
										Looper.loop();
										return;
									}
								 else
								 {
										udpSocket=null;
										Looper.prepare();
										Toast.makeText(getApplicationContext(), "failed",Toast.LENGTH_LONG).show();
										Looper.loop();
									 
								 }
									
								}
								
							} catch (Exception e) 
							{
							// TODO Auto-generated catch block
								e.printStackTrace();
							}

					}
				}).start();

			}
		});
		
		
		
		m_btnCancel.setOnClickListener(new Button.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				

				
				//finish();


			}
		});
		
	}

	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.re_pwd, menu);
		return true;
	}

}
